<template>
  <div class="loading"></div>
</template>

<script setup lang="ts">
import { cxloginApi } from "@/api/loadingLogin/index";
import { isRes } from "@/utils/pub-use";
import { userStore } from "@/store/user";
import { getEncrypt_token } from "@/utils/auth";
import { onMounted } from "vue";

import useCommonStore from "@/store/common/index";
import { useRoute, useRouter } from "vue-router";

const commonStore = useCommonStore();
commonStore.siteConfigAction();
const router = useRouter();
const route = useRoute();
let type: any = route.query?.type; //1团长2商品 3一键开团
let encrypt_token: any = "";

const getToken = async () => {
  encrypt_token = getEncrypt_token();

  if (encrypt_token) {
    let formData = new FormData();
    formData.append("encrypt_token", encrypt_token);
    let res = await cxloginApi(formData);
    if (isRes(res)) {
      userStore().setToken(res.result?.token);
      commonStore.siteConfigAction();
      if (type == 1) {
        router.push({
          path: "/home/commander",
          query: {
            id: route.query?.id,
          },
        });
      } else if (type == 2) {
        router.push({
          path: "/home/groupLink",
          query: {
            id: route.query?.id,
            userId: route.query?.userId,
          },
        });
      } else if (type == 3) {
        router.push({
          path: "/add",
        });
      } else {
        router.push("/home");
      }
    }
  }
};
//是否是第一次进入
const isFirstFn = () => {
  if (!localStorage.getItem("wtgFirst")) {
    localStorage.setItem("wtgFirst", "true");
  }
};
onMounted(() => {
  getToken();
  isFirstFn();
});
</script>

<style lang="scss" scoped>
.loading {
  width: 100%;
  height: 100%;
}
</style>
